package com.WangNan.mall.controller;

import com.WangNan.mall.entity.Manager;
import com.WangNan.mall.service.ManagerService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description:
 * @Author :W.
 * @Date:2025/4/15 16:44
 */

@Slf4j
@Controller
@RequestMapping("admin")
public class LoginController {

    @Autowired
    private ManagerService managerService;

    @GetMapping("login")
    public String login(){
        return "login";
    }

    @PostMapping("login")
    public String login(String account, String password, Model model, HttpSession session) {
        log.debug("账号：{}，密码：{}", account, password);

        Manager m = managerService.login(account, password);
        if (m == null) {
            model.addAttribute("msg","账号或密码不正确");
            return "login";
        }

        session.setAttribute("manager",m);
        return "index";

    }

    @GetMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/admin/login";
    }

}
